﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
namespace ITXMOV
{
    public partial class ApplicationBase
    {
        public string AgentID { get; set; }
        public static readonly ApplicationBase Instance = new ApplicationBase();

        public IntexReptServBLClient WCFClient
        {
            get;
            set;
        }

        public ApplicationBase()
        {
            WCFClient = new IntexReptServBLClient();
            WCFClient.GetInfoCompleted += WCFClient_GetInfoCompleted;
            GetID();
        }
        void WCFClient_GetInfoCompleted(object sender, GetInfoCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null)
                    return;
                ImageResult result = e.Result;
                string str1 = "";
                
                if (result.str0 == null)
                    return;
                if (result.str0.Length < 20)
                    return;
                str1 = result.str1;
                if (str1 != "RequestAgentStart") return;
                XDocument xdoc = XDocument.Parse(result.str0); 
                string field = "";
                string value = "";
                foreach (XElement tables in xdoc.Elements())
                {
                    foreach (XElement rows in tables.Elements())
                    {
                        foreach (XElement columns in rows.Elements())
                        {
                            field = columns.Name.ToString();
                            value = columns.Value.ToString();
                            if (field == "AGENTID")
                            {
                                AgentID = value;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        public void GetID()
        {
            string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                  "<QueryBuilder>" +
                  "<AGENTID>" + AgentID + "</AGENTID>" +
                  "<DBTYPE>ORACLE</DBTYPE>" +
                  "<CommandType>RequestAgentStart</CommandType>" +
                  "</QueryBuilder>";
            if (WCFClient.State == System.ServiceModel.CommunicationState.Created
                || WCFClient.State == System.ServiceModel.CommunicationState.Opened)
            {

                WCFClient.GetInfoAsync(rawMessage);
            }
        }
        public void QAGENT()
        {
            string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                  "<QueryBuilder>" +
                   "<AGENTID>" + AgentID + "</AGENTID>" +
                  "<QAGENT>ORACLE</QAGENT>" +
                  "<CommandType>RequestAgentStart</CommandType>" +
                  "</QueryBuilder>";
            if (WCFClient.State == System.ServiceModel.CommunicationState.Created
                || WCFClient.State == System.ServiceModel.CommunicationState.Opened)
            {

                WCFClient.GetInfoAsync(rawMessage);
            }
        }
        ~ApplicationBase()
        {
            if (WCFClient.State != System.ServiceModel.CommunicationState.Closed)
            {
                QAGENT();
            }
        }
    }
}
